import sys, os 
RootDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(RootDir)

from weiqi.dlgo.utils import R as DR
from keras.layers import Input, Dense
from keras.models import Model
from weiqi.dlgo.encoder import SimpleEncoder
from weiqi.dlgo.networks.large import Layers
from weiqi.dlgo.agent import ACAgent
import h5py

def run_acagent():
    encoder = DR.GetEncoderByName('simple', 19)
    board_input = Input(shape=encoder.Shape(), name='board_input')
    processed_board = board_input
    for layer in Layers(encoder.Shape()):
        processed_board = layer(processed_board)

    policy_hidden_layer = Dense(512, activation='relu')(processed_board)
    policy_output = Dense(encoder.NumberPoints(), activation='softmax')(policy_hidden_layer)
    value_hidden_layer = Dense(512, activation='relu')(processed_board)
    value_output = Dense(1, activation='tanh')(value_hidden_layer)
    
    model = Model(inputs=[board_input], outputs=[policy_output, value_output])
    
    agent = ACAgent(model, encoder)
    with h5py.File('acagent.h5', 'w') as f:
        agent.Serialize(f)